package com.mycompany.ui.panel;

import org.springframework.beans.factory.annotation.Autowired;

import com.mycompany.config.Injector;
import com.mycompany.model.User;
import com.mycompany.service.UserService;
import com.vaadin.data.fieldgroup.BeanFieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.fieldgroup.PropertyId;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

public class UserLockTestPanel extends VerticalLayout {
	
	private User user = new User();
	TextField fieldId = new TextField("Inserisci id Utente da cercare");
	
	Button buttonCerca = new Button("Cerca");
	Button buttonModifica = new Button("Modifica");
	
	FieldGroup fieldGroup = new BeanFieldGroup<User>(User.class);
	
	@PropertyId("nome")
	TextField fieldNome = new TextField("Nome");
	
	@PropertyId("cognome")
	TextField fieldCognome = new TextField("Cognome");
	
	@PropertyId("codiceFiscale")
	TextField fieldCodiceFiscale = new TextField("CodiceFiscale");
	
	@PropertyId("email")
	TextField fieldMail = new TextField("Mail");
	
	Label label = new Label();
	
	
	@Autowired
	UserService userService;
	
	public UserLockTestPanel() {
		// TODO Auto-generated constructor stub
		Injector.inject(this);
		
		fieldGroup.setItemDataSource(new BeanItem<User>(new User()));
		fieldGroup.bindMemberFields(this);
		
		
		addComponent(createPanelInsertIdUser());
		addComponent(createResultsPanel());
		addListeners();
	}



	private void addListeners() {
		// TODO Auto-generated method stub
		buttonCerca.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				user = userService.findOne(new Long(Long.parseLong(fieldId.getValue())));
				//setFieldsValue();
				fieldGroup.setItemDataSource(new BeanItem<User>(user));
				label.setValue("");
			}
		});
		
		buttonModifica.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				// TODO Auto-generated method stub
				try {
					fieldGroup.commit();
					fieldGroup.setBuffered(false);
					userService.saveUser(user);
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					label.setValue("Impossibile Salvare record modificato da un'altra transazione");
					
				}
				
			}
		});
		
		
	}



	private Component createPanelInsertIdUser() {
		// TODO Auto-generated method stub
		HorizontalLayout horizontalLayout = new HorizontalLayout();
		horizontalLayout.addComponent(fieldId);
		horizontalLayout.addComponent(buttonCerca);
		return horizontalLayout;
	}
	
	private Component createResultsPanel() {
		// TODO Auto-generated method stub
		GridLayout gridLayout = new GridLayout(2,3);
		setFieldsValue();
		gridLayout.addComponent(fieldNome);
		gridLayout.addComponent(fieldCognome);
		gridLayout.addComponent(fieldMail);
		gridLayout.addComponent(fieldCodiceFiscale);
		gridLayout.addComponent(buttonModifica);
		gridLayout.addComponent(label);
		return gridLayout;
	}



	private void setFieldsValue() {
		// TODO Auto-generated method stub
		fieldNome.setValue(user.getNome());
		fieldCognome.setValue(user.getCognome());
		fieldMail.setValue(user.getEmail());
		fieldCodiceFiscale.setValue(user.getCodiceFiscale());
	}

}
